<template>
	<view class="my-path-list">
		<view class="path-list">
			<view class="path-item" v-for="(item,index) in getPath" :key="index">
				<view class="path-info" @tap="toAddPath(item,index)">
					<view class="item-main">
						<view class="item-name">{{item.name}}</view>
						<view class="">{{item.tel}}</view>
					</view>
					<view class="item-main">
						<view class="active" v-if="item.isDefault">默认</view>
						<view class="">{{item.city}} {{item.details}}</view>
					</view>
				</view>
				<view class="path-btn" @tap="detelePath(index)">删除</view>
			</view>
		</view>
		<view class="add-path">
			<view class="add-path-btn" @tap="goAddPath">新增地址</view>
		</view>
	</view>
</template>

<script>
	import $http from "@/common/api/request.js"
	export default {
		data() {
			return {
				isSelectedPath: false,
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#007aff'
					}
				}]
			};
		},
		onLoad(e) {
			if (e.type == 'selectedPath') {
				this.isSelectedPath = true
			}
		},
		//初始化数据()
		methods: {
			//跳转新增地址
			goAddPath() {
				uni.navigateTo({
					url: "/pages/my-add-path/my-add-path"
				})
			},
			//修改地址/选择默认地址
			toAddPath(item, index) {
				//选择默认地址
				if (this.isSelectedPath) {
					//切换默认地址
					this.$store.commit('defaultPath', item)
					//返回上一级页面
					uni.navigateBack({
						delta: 1
					})
				} else {
					//修改地址
					let pathObj = JSON.stringify({
						index,
						item: this.getPath[index]
					})
					uni.navigateTo({
						url: "/pages/my-add-path/my-add-path?data=" + pathObj
					})
				}
			},
			detelePath(index){
				uni.showModal({
					title:"重要提示",
					content:"是否删除此地址",
					success:res=>{
						if(res.confirm){
							this.$store.commit('deletePath',index)
						}
					}
				})
			}
		},
		computed: {
			getPath() {
				return this.$store.state.path.list
			}
		}
	}
</script>

<style lang="scss" scoped>
	.my-path-list {
		.path-list {
			padding-left: 20rpx;

			.path-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx;
				border-bottom: 2rpx solid #ccc;

				.item-main {
					display: flex;
					align-items: center;

					.active {
						padding: 0 6rpx;
						background-color: #49bdfb;
						color: #fff;
						border-radius: 26rpx;
						font-size: 24rpx;
						margin-right: 10rpx;
					}

					.item-name {
						padding-right: 10rpx;
					}
				}
				.path-btn{
					padding: 10rpx 26rpx;
					color: #fff;
					margin-right: 10rpx;
					background-color: rgb(245, 108, 108);
				}
			}
		}

		.add-path {
			display: flex;
			justify-content: center;
			width: 100%;
			padding: 20rpx 0;

			.add-path-btn {
				border: 2rpx solid #49bdfb;
				color: #49bdfb;
				border-radius: 30rpx;
				padding: 6rpx 60rpx;
			}
		}
	}
</style>
